package com.arrogames.rgblabyrinth.menu.components {
	import com.arrogames.rgblabyrinth.AssetManager;
	import com.arrogames.rgblabyrinth.animations.OptionsAnimationBuilder;
	import com.arrogames.rgblabyrinth.menu.events.OptionsMenuEvent;
	import com.arrogames.util.UIUtil;
	
	import feathers.controls.LayoutGroup;
	import feathers.controls.ToggleButton;
	
	import starling.display.Button;
	import starling.events.Event;
	
	public class OptionsMenu extends LayoutGroup {
		
		public static var GAME:String = "game";
		public static var GAME_PAUSE:String = "game_pause";
		public static var MENU:String = "menu";
		
		public var playButton:ToggleButton;
		public var musicButton:ToggleButton;
		public var sfxButton:ToggleButton;
		public var exitButton:Button;
		
		private var currentState:String;
		private var animationManager:OptionsAnimationBuilder;
		
		public function OptionsMenu() {
			super();
			animationManager = new OptionsAnimationBuilder(this);
			
			currentState = MENU;
		}
		
		public function updateState(newState:String):void {
			if (newState != currentState) {
				currentState = newState
				if (currentState == MENU) {
					animationManager.showButtons([musicButton, sfxButton]);
					
				} else if (currentState == GAME) {
					animationManager.showButtons([playButton]);
					
				} else if (currentState == GAME_PAUSE) {
					animationManager.showButtons([playButton, musicButton, sfxButton, exitButton]);
				}
				animationManager.create().play();
			}
			invalidate();
		}
		
		override protected function initialize():void {
			super.initialize();
			
			var index:int=0;
			playButton = UIUtil.createToggleButton(
				AssetManager.instance.getTexture("game_play"),
				AssetManager.instance.getTexture("game_pause"),
				(index++ * (24+5)),0,onTogglePause);
			playButton.alpha = 0;
			
			musicButton = UIUtil.createToggleButton(
				AssetManager.instance.getTexture("music_mute"),
				AssetManager.instance.getTexture("music_unmute"),
				(index++ * (24+5)),0,onToggleMusic);
			
			sfxButton = UIUtil.createToggleButton(
				AssetManager.instance.getTexture("sfx_mute"),
				AssetManager.instance.getTexture("sfx_unmute"),
				(index++ * (24+5)),0,onToggleSFX);
			
			exitButton = UIUtil.createButton(
				AssetManager.instance.getTexture("game_exit"),
				(index++ * (24+5)),0,onExitGame);
			exitButton.alpha = 0;
			
			addChild(exitButton);
			addChild(musicButton);
			addChild(sfxButton);
			addChild(playButton);
		}
		
		private function onTogglePause(event:Event):void {
			if (currentState == GAME) {
				updateState(GAME_PAUSE);
			} else if (currentState == GAME_PAUSE) {
				updateState(GAME);
			}
			dispatchEvent(new OptionsMenuEvent(OptionsMenuEvent.TOGGLE_PAUSE));
		}
		
		private function onToggleMusic(event:Event):void {
			dispatchEvent(new OptionsMenuEvent(OptionsMenuEvent.TOGGLE_MUSIC));
		}
		
		private function onToggleSFX(event:Event):void {
			dispatchEvent(new OptionsMenuEvent(OptionsMenuEvent.TOGGLE_SFX));
		}
		
		private function onExitGame(event:Event):void {
			dispatchEvent(new OptionsMenuEvent(OptionsMenuEvent.EXITGAME));
		}
	}
}